package drds.server.net.packet;

import java.io.UnsupportedEncodingException;

import drds.server.config.Fields;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class BindValue {

	public static final void bindValue(BytesHolder bytesHolder, Value value, String charset) throws UnsupportedEncodingException {
		switch (value.type & 0xff) {
		case Fields.FIELD_TYPE_BIT:
			value.value = bytesHolder.readBytesWithLength();
			break;
		case Fields.FIELD_TYPE_INT:// old FIELD_TYPE_LONG
			value.$int = bytesHolder.readInt4();
			break;
		case Fields.FIELD_TYPE_LONG:// old FIELD_TYPE_LONG_LONG
			value.$long = bytesHolder.readLong8();
			break;
		case Fields.FIELD_TYPE_DATE:
		case Fields.FIELD_TYPE_DATETIME:
		case Fields.FIELD_TYPE_TIMESTAMP:
			value.value = bytesHolder.readDate();
			break;
		case Fields.FIELD_TYPE_VAR_STRING:
		case Fields.FIELD_TYPE_STRING:
		case Fields.FIELD_TYPE_VARCHAR:
			value.value = bytesHolder.readStringWithLength(charset);

			break;
		case Fields.FIELD_TYPE_DECIMAL:
		case Fields.FIELD_TYPE_NEW_DECIMAL:
			value.value = bytesHolder.readBigDecimal();

			break;
		default:
			throw new IllegalArgumentException("bindValue error,unsupported type:" + value.type);
		}
		value.isSet = true;
	}

}